package org.ruoyi.gathe.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.ruoyi.common.core.utils.MapstructUtils;
import org.ruoyi.common.core.utils.StringUtils;
import org.ruoyi.common.mybatis.core.page.PageQuery;
import org.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.ruoyi.gathe.domain.GatheTableColumn;
import org.ruoyi.gathe.domain.bo.GatheTableColumnBo;
import org.ruoyi.gathe.domain.vo.GatheTableColumnVo;
import org.ruoyi.gathe.mapper.GatheTableColumnMapper;
import org.ruoyi.gathe.service.IGatheTableColumnService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 采集字段Service业务层处理
 *
 * @author bz_huaYal9146
 * @date 2025-11-06
 */
@RequiredArgsConstructor
@Service
public class GatheTableColumnServiceImpl implements IGatheTableColumnService {

    private final GatheTableColumnMapper baseMapper;

    /**
     * 查询采集字段
     *
     * @param id 主键
     * @return 采集字段
     */
    @Override
    public GatheTableColumnVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询采集字段列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 采集字段分页列表
     */
    @Override
    public TableDataInfo<GatheTableColumnVo> queryPageList(GatheTableColumnBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GatheTableColumn> lqw = buildQueryWrapper(bo);
        Page<GatheTableColumnVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的采集字段列表
     *
     * @param bo 查询条件
     * @return 采集字段列表
     */
    @Override
    public List<GatheTableColumnVo> queryList(GatheTableColumnBo bo) {
        LambdaQueryWrapper<GatheTableColumn> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GatheTableColumn> buildQueryWrapper(GatheTableColumnBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GatheTableColumn> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(GatheTableColumn::getId);
        lqw.eq(bo.getGatheTableId() != null, GatheTableColumn::getGatheTableId, bo.getGatheTableId());
        lqw.eq(StringUtils.isNotBlank(bo.getGatheColumn()), GatheTableColumn::getGatheColumn, bo.getGatheColumn());
        lqw.like(StringUtils.isNotBlank(bo.getGatheColumnName()), GatheTableColumn::getGatheColumnName, bo.getGatheColumnName());
        lqw.eq(StringUtils.isNotBlank(bo.getGatheColumnFar()), GatheTableColumn::getGatheColumnFar, bo.getGatheColumnFar());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPk()), GatheTableColumn::getIsPk, bo.getIsPk());
        return lqw;
    }

    /**
     * 新增采集字段
     *
     * @param bo 采集字段
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(GatheTableColumnBo bo) {
        GatheTableColumn add = MapstructUtils.convert(bo, GatheTableColumn.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改采集字段
     *
     * @param bo 采集字段
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(GatheTableColumnBo bo) {
        GatheTableColumn update = MapstructUtils.convert(bo, GatheTableColumn.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GatheTableColumn entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除采集字段信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
